#include "BackgroundIsles.h"

BackgroundIsles::BackgroundIsles(GameState *gameState) : GameObject(gameState){
	m_isles.push_back(BackgroundIsleInfo(12.0f, 3.0f, 0.0f));
	m_isles.push_back(BackgroundIsleInfo(8.0f, -6.0f, 0.0f));
	m_isles.push_back(BackgroundIsleInfo(-13.0f, -3.0f, 0.0f));
	m_isles.push_back(BackgroundIsleInfo(-5.0f, 9.0f, 0.0f));
	m_isles.push_back(BackgroundIsleInfo(18.0f, 12.0f, 0.2f));
	m_isles.push_back(BackgroundIsleInfo(2.0f, -4.0f, 0.2f));
	m_isles.push_back(BackgroundIsleInfo(-0.5f, 4.0f, 0.2f));
	m_isles.push_back(BackgroundIsleInfo(-3.0f, -14.0f, 0.2f));
	m_isles.push_back(BackgroundIsleInfo(8.0f, 15.0f, 0.25f));
	m_isles.push_back(BackgroundIsleInfo(2.0f, -3.0f, 0.3f));
	m_isles.push_back(BackgroundIsleInfo(-18.0f, -12.0f, 0.35f));
	m_isles.push_back(BackgroundIsleInfo(1.0f, 8.0f, 0.38f));
	m_isles.push_back(BackgroundIsleInfo(-14.0f, 10.0f, 0.4f));
	m_isles.push_back(BackgroundIsleInfo(-6.0f, 4.0f, 0.42f));
	m_isles.push_back(BackgroundIsleInfo(19.0f, -6.0f, 0.5f));
	m_isles.push_back(BackgroundIsleInfo(-4.0f, -8.0f, 0.55f));
	setLayer(8);
	
	m_texture = gameState->getTextureManager().getTexture("backgroundIsle1.png");
}

void BackgroundIsles::update(double dt){
	
}


void BackgroundIsles::draw(){
	float width = m_gameState->camera.widthUnits;
	float height = m_gameState->camera.heightUnits;
	height+=2;

	sf::Texture::bind(m_texture);

	for(unsigned int i = 0; i < m_isles.size(); i++){
		glPushMatrix();
		{
			float posX = m_gameState->camera.posX*(m_isles[i].depth)+m_isles[i].xPos;
			float posY = m_gameState->camera.posY*(m_isles[i].depth)+m_isles[i].yPos;

			 
			while(posY > height/2.0f){
				posY-=height;
			}

			float sizeX = (m_isles[i].depth)*10+1.0f;
			float sizeY = sizeX * (float(m_texture->getSize().y)/m_texture->getSize().x);

			float depth = -(1-m_isles[i].depth)*5;

			glTranslatef(m_gameState->camera.posX, m_gameState->camera.posY,0);
			glTranslatef(-posX, -posY, 0); 

			glScalef(sizeX, sizeY, 1);
			glColor3f((1.0f-m_isles[i].depth)*1.0f+0.7f, (1.0f-m_isles[i].depth)*1.0f+0.7f, (1.0f-m_isles[i].depth)*3.0f+0.8f);
			glBegin(GL_QUADS);
			{
				glTexCoord2f(0.0f, 1.0f); glVertex3f( -0.5f, -0.5f, depth);
				glTexCoord2f(1.0f, 1.0f); glVertex3f( 0.5f, -0.5f, depth);
				glTexCoord2f(1.0f, 0.0f); glVertex3f( 0.5f, 0.5f, depth);
				glTexCoord2f(0.0f, 0.0f); glVertex3f( -0.5f, 0.5f, depth);
			}
			glEnd();
		}
		glPopMatrix();
	}

	sf::Texture::bind(NULL);
	
	
}


